package com.shou.infrastructure.persistent.repository.park;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.park.model.ParkingLotEntity;
import com.shou.domain.park.repository.IParkingLotRepository;
import com.shou.infrastructure.persistent.dao.IParkingLotDao;
import com.shou.infrastructure.persistent.po.ParkingLotPO;
import com.shou.infrastructure.persistent.repository.park.mapstruct.ParkingLotPOConvert;
import jakarta.annotation.Resource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
* @author Nemo
* @description 针对表【ParkinglotPO(停车场表，记录停车场基本信息)】的数据库操作Service
* @createDate 2025-02-07 22:10:51
*/
@Repository("parkingLotRepository")
public class ParkingLotRepository implements IParkingLotRepository {
    @Resource
    private IParkingLotDao parkingLotDao;
    @Resource
    private ParkingLotPOConvert parkingLotPOConvert;
    @Override
    public void insert(ParkingLotEntity parkingLot) {
        ParkingLotPO parkingLotPO = parkingLotPOConvert.convertParkingLotEntityToPO(parkingLot);
        try {
            parkingLotDao.insertSelective(parkingLotPO);
        } catch (DataIntegrityViolationException e) {
            Throwable rootCause = e.getRootCause();
            if (rootCause instanceof SQLException && "22001".equals(((SQLException) rootCause).getSQLState())) {
                throw new IllegalArgumentException("输入数据过长导致SQL执行失败: " + rootCause.getMessage());
            } else {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }

    @Override
    public void updateParkingLotById(ParkingLotEntity parkingLot) {
        ParkingLotPO parkingLotPO = parkingLotPOConvert.convertParkingLotEntityToPO(parkingLot);
        parkingLotDao.updateByPrimaryKeySelective(parkingLotPO);
    }

    @Override
    public ParkingLotEntity getParkingLotById(Integer id) {
        ParkingLotPO parkingLotPO = parkingLotDao.selectById(id);
        return parkingLotPOConvert.convertParkingLotPOToEntity(parkingLotPO);
    }

    @Override
    public IPage<ParkingLotEntity> getParkingLotsPage(PageQueryEntity pageQueryEntity) {
        // 根据页码和页面大小,构建分页对象
        Page<ParkingLotPO> page = new Page<>(pageQueryEntity.getPageIndex(), pageQueryEntity.getPageSize());
        // 采用模糊查询
        IPage<ParkingLotPO> pageMessage = parkingLotDao.selectPageMessageByCondition(
                page,
                pageQueryEntity.getName(),
                pageQueryEntity.getAddress(),
                pageQueryEntity.getLatitude(),
                pageQueryEntity.getLatitude(),
                pageQueryEntity.getLongitude(),
                pageQueryEntity.getOperatingHours(),
                pageQueryEntity.getPhone(),
                pageQueryEntity.getFeeScheduleCode(),
                pageQueryEntity.getDescription(),
                pageQueryEntity.getSortName(),
                pageQueryEntity.getSort()
        );
        return parkingLotPOConvert.convertParkingLotPOPageToEntity(pageMessage);
    }
}
